<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
     <el-row :gutter="24">
        <el-col :span="12" >
            <el-form-item label="月份" prop="materialParam">
                    <el-date-picker
                         type="month"
            v-model="currentMonth"
                         placeholder="选择月" :clearable=false>
                       </el-date-picker>
                 </el-form-item>
                  <el-form-item label="商品信息" prop="barCode">
                 <el-input v-model="queryParams.materialParam" placeholder="条码/名称/规格/型号" clearable size="small"
                   @keyup.enter.native="handleQuery" />
                   </el-form-item>
                 <el-form-item>
                   <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                   <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                   <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
                 </el-form-item>
        </el-col>
        <el-col :span="8" >
            <el-form-item>
                    注：本报表只含采购数据
                  </el-form-item>
        </el-col>
     </el-row>
     <!--  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
    </el-form>



    <el-table v-loading="loading" max-height="620px" :data="buyInReportList" @selection-change="handleSelectionChange"
    :summary-method="getSummaries"
       show-summary>
      <el-table-column type="selection" width="65" align="center" />
      <el-table-column label="条码" align="center" prop="barCode" />
      <el-table-column label="名称" align="center" prop="materialName" />
      <el-table-column label="规格" align="center" prop="materialStandard" />
      <el-table-column label="型号" align="center" prop="materialModel" />
      <el-table-column label="扩展信息" align="center" prop="materialUnit" />
<el-table-column label="单位" align="center" prop="materialOther" />
<el-table-column label="采购数量" align="center" prop="inSum" />
<el-table-column label="采购金额" align="center" prop="inSumPrice" />
<el-table-column label="退货数量" align="center" prop="outSum" />
<el-table-column label="退货金额" align="center" prop="outSumPrice" />
<el-table-column label="实际采购金额" align="center" prop="inOutSumPrice" />

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      :page-sizes="[10, 50, 100, 200,500, 1000,2000]"
      @pagination="getList"
    />


  </div>
</template>

<script>
import { buyIn } from "@/api/bill/retail";
import * as math from 'mathjs'
    export default {
  name: "BuyInReport",
  data() {
    return {
      currentMonth:this.$moment().format("YYYY-MM"),
monthFormat: 'YYYY-MM',
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
     // 采购统计
     buyInReportList: [],
     // 弹出层标题
     title: "",
     // 是否显示弹出层
     open: false,
     // 查询参数
     queryParams: {
       pageNum: 1,
       pageSize: 10,
       monthTime: null,
        materialParam: "",
          mpList: "",
       serialNo: null

     },
    allMonthAmount: '',
    allCurrentAmount: '',
      // 表单参数
      form: {},
      // 表单校验
      rules: {

      }
    };
  },
  created() {
   this.getList();
  },
  methods: {
    getSummaries(param) {
                   const { columns, data } = param;
                   const sums = [];
                   columns.forEach((column, index) => {
                     if (index === 0) {
                       sums[index] = '合计';
                       return;
                     }
                     if (index === 1) {
                       sums[index] = '-';
                       return;
                     }
    if (index === 2) {
                       sums[index] = '-';
                       return;
                     }
                     if (index === 3) {
                       sums[index] = '-';
                       return;
                     }
                     if (index === 4) {
                       sums[index] = '-';
                       return;
                     }
                     if (index === 5) {
                       sums[index] = '-';
                       return;
                     }

                     if (index === 6) {
                       sums[index] = '-';
                       return;
                     }
                     const values = data.map(item => Number(item[column.property]));
                     if (!values.every(value => isNaN(value))) {
                       sums[index] = values.reduce((prev, curr) => {
                         const value = Number(curr);
                          if (!isNaN(value)) {
                            return math.add(math.bignumber(prev),math.bignumber(curr));
                          } else {
                            return math.bignumber(prev);
                          }
                       }, 0);
                       sums[index] += '';//可以写单位
                     }
                   });

                   return sums;
                 },
onChange: function (value, dateString) {
        this.queryParam.monthTime=dateString;
      },
    /** 查询库存列表 */
    getList() {
     this.loading = true;

     // console.log(this.queryParams.monthTime)
 this.queryParams.monthTime=this.$moment(this.currentMonth).format("YYYY-MM");
     if(this.queryParams.monthTime==null||this.queryParams.monthTime==""){
       this.queryParams.monthTime=this.$moment().format("YYYY-MM");
     }
     buyIn(this.queryParams).then(response => {
       this.buyInReportList = response.data;
       this.total = response.total;
       this.loading = false;
     });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMaterialunit(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改计量单位";
      });
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('/bill/retail/buyInExport', {
        ...this.queryParams
     }, `采购统计_${this.parseTime(new Date(),'{y}-{m}-{d}')}.xlsx`)
    }
  }
};
</script>
